﻿using DeviceMonitor.Dao;
using DeviceMonitor.Model;
using DeviceMonitor.Utils;
using DeviceMonitor.ViewModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceMonitor.Pages
{
    /// <summary>
    /// AlermPage.xaml 的交互逻辑
    /// </summary>
    public partial class AlermPage : Page
    {
        AlermViewModel viewModel;
        SynchronizationContext _syncContext = null;

        public AlermPage()
        {
            InitializeComponent();
            _syncContext = SynchronizationContext.Current;
            viewModel = new AlermViewModel();

            viewModel.queryParam.timeBeggin = DateTime.Now.ToString("yyyy-MM-dd");
            viewModel.queryParam.timeEnd = DateTime.Now.ToString("yyyy-MM-dd");
            
            this.DataContext = viewModel;

            this.Loaded += Page_loaded;
        }
        private void Page_loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }
        private void loadData()
        {
            /*
            Hashtable paraMap = new Hashtable();
            paraMap.Add("queryStartDate", viewModel.queryParam.timeBeggin);
            paraMap.Add("queryEndDate", viewModel.queryParam.timeEnd);

            ArrayList list = (ArrayList)SqlMapDao.instance().QueryForList("selectAlerms", paraMap);
            viewModel.list.Clear();
            foreach (AlermModel alerm in list)
            {
                viewModel.list.Add(alerm);
            }*/
            new Task(() =>
            {
                try
                {
                    JObject paramJSON = new JObject();
                    paramJSON.Add("queryStartDate", viewModel.queryParam.timeBeggin);
                    paramJSON.Add("queryEndDate", viewModel.queryParam.timeEnd);
                    String response = HttpPostHelper.doHttpPost("/alerm/queryList", paramJSON.ToString());
                    JObject rspJson = JObject.Parse(response);
                    bool success = (bool)rspJson["success"];
                    if (success)
                    {
                        var jsonStr = rspJson["data"].ToString();
                        List<AlermModel> items = JsonConvert.DeserializeObject<List<AlermModel>>(jsonStr);  

                        _syncContext.Post(SetDataList, items);
                    }
                }
                catch (Exception ex) { }
            }).Start();
        }
        private void SetDataList(Object obj)
        {
            List<AlermModel> items = (List<AlermModel>)obj;
            viewModel.list.Clear();
            foreach(AlermModel item in items)
            {
                viewModel.list.Add(item);
            }
        }
        private void Button_Click_Query(object sender, RoutedEventArgs e)
        {
            loadData();
        }

        private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
